<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class Gold_priceController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/gold_price/index';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function index(){
        $gold_type = config('app.gold_type');
        $list = DB::table('gold_price')->orderBy('sort_order','desc')->orderBy('id','desc')->paginate(15);
        // dd($list);exit;
        return view('backstage/gold_price/index',['list'=>$list,'gold_type'=>$gold_type]);
    }
    public function add(){
        $gold_type = config('app.gold_type');
        return view('backstage/gold_price/add',['gold_type'=>$gold_type]);
    }
    public function doadd(Request $request){
        if($request->isMethod('POST')){
            
            $data = $request->all();

            $gold['name'] = $data['name'];
            $gold['buy_price'] = $data['buy_price'];
            $gold['sale_price'] = $data['sale_price'];
            $gold['type'] = intval($data['type']);
            if(empty($data['name']) || empty($data['buy_price']) || empty($data['sale_price']) || empty($data['type'])){
                return json_encode(array('status' => 0, 'message'=>'提交失败，缺少必要参数'));
            }
            if(empty($data['sort_order'])){
                $gold['sort_order'] = 0;
            }else{
                $gold['sort_order'] = intval($data['sort_order']);
            }
            $gold['is_show'] = $data['is_show'];
            $gold['add_time'] = time();
            $newid = CommonModel::addDataGetId('gold_price',$gold);
            if($newid == true){
                $arrJson = array('status' => 1, 'message'=>'添加成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'添加失敗');
            }
            return json_encode($arrJson);
        }
    }
    public function edit($id){
        $table = 'gold_price';
        $data =['id'=>$id];
        $gold = CommonModel::getOne($table,$data);
        $gold_type = config('app.gold_type');
        return view('backstage/gold_price/edit',['gold'=>$gold,'gold_type'=>$gold_type]);
    }
    public function doedit(Request $request){
        if($request->isMethod('POST')){
            
            $data = $request->all();
            $id = intval($data['id']);
            $gold['name'] = $data['name'];
            $gold['buy_price'] = $data['buy_price'];
            $gold['sale_price'] = $data['sale_price'];
            $gold['type'] = intval($data['type']);
            if(empty($data['name']) || empty($data['buy_price']) || empty($data['sale_price']) || empty($data['type'])){
                return json_encode(array('status' => 0, 'message'=>'提交失败，缺少必要参数'));
            }
            if(empty($data['sort_order'])){
                $gold['sort_order'] = 0;
            }else{
                $gold['sort_order'] = intval($data['sort_order']);
            }
            $gold['is_show'] = $data['is_show'];
            $save = CommonModel::updateData('gold_price',['id'=>$id],$gold);
            if($save == true){
                $arrJson = array('status' => 1, 'message'=>'修改成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'修改失敗');
            }
            return json_encode($arrJson);
        }
    }
    



}
